﻿using JT809.Protocol;
using JTT809_2011_Service.DotNettyHandle;
using JTT809_2011_Service.JsonConverters;
using JTT809_2011_Service.JT809;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JTT809_2011_Service.DotnettyExtensions
{
    public static class JT809CoreDotnettyExtensions
    {
        static JT809CoreDotnettyExtensions()
        {
            //配置默认格式化
            JsonConvert.DefaultSettings = new Func<JsonSerializerSettings>(() =>
            {
                JsonSerializerSettings settings = new JsonSerializerSettings();
                //日期类型默认格式化处理
                settings.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
                settings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                //空值处理
                settings.NullValueHandling = NullValueHandling.Ignore;
                settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                //自定义格式化处理
                settings.Converters.Add(new JsonIPAddressConverter());
                settings.Converters.Add(new JsonIPEndPointConverter());

                return settings;
            });
        }

        public static IJT809Builder AddJT809Core(this IJT809Builder builder,IConfiguration configuration, JsonSerializerSettings settings = null)
        {
            if (settings != null)
            {
                JsonConvert.DefaultSettings = new Func<JsonSerializerSettings>(() =>
                {
                    settings.Converters.Add(new JsonIPAddressConverter());
                    settings.Converters.Add(new JsonIPEndPointConverter());
                    settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                    return settings;
                });
            }
            builder.Services.Configure<JT809Configuration>(configuration.GetSection("JT809Configuration"));
            //JT809计数器服务工厂
            builder.Services.TryAddSingleton<JT809AtomicCounterServiceFactory>();
            //JT809编解码器
            builder.Services.TryAddScoped<JT809Decoder>();
            builder.Services.TryAddScoped<JT809Encoder>();
            return builder;
        }
        /// <summary>
        /// 下级平台
        /// 主链路为客户端
        /// 从链路为服务端
        /// </summary>
        /// <returns></returns>
        public static IJT809Builder AddJT809InferiorPlatformClient(this IJT809Builder builder)
        {
            builder.Services.TryAddSingleton<IJT809ManualResetEvent, JT809InferoprManualResetEvent>();
            //主从链路客户端和服务端连接处理器
            builder.Services.TryAddScoped<JT809MainClientConnectionHandler>();
            //主从链路接收消息默认业务处理器
            builder.Services.TryAddSingleton<JT809InferiorMsgIdReceiveHandlerBase, JT809InferiorMsgIdReceiveDefaultHandler>();
            //主从链路消息接收处理器
            builder.Services.TryAddScoped<JT809MainClientHandler>();
            //主链路客户端
            builder.Services.TryAddSingleton<JT809MainClient>();
            return builder;
        }
    } 
}
